/*
   Copyright(c) 2010, 2011 Bryce Alcock

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.com

 */
package singleton;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Double check lock safe lazy loaded singleton.
 *
 * @author Bryce.Alcock  
 *
 */
public class SingletonClassic implements Serializable {

    // The single instance.
    private static SingletonClassic instance;
    
    public boolean isReadyInstance =false;
    public static final AtomicInteger constructorInteger = new AtomicInteger(0);

    public final  int instanceid=constructorInteger.incrementAndGet();
    public static SingletonClassic getInstance(){
        // If the instance is null, we must create it or wait for another thread
        // to create it.
        if(instance==null){
            synchronized(SingletonClassic.class){
               if(instance==null){
                    instance =new SingletonClassic();
                }
            }
        }
        return instance;
    }

    private SingletonClassic(){
        
        try {
            Thread.sleep(10);
        } catch (InterruptedException ex) {
            Logger.getLogger(SingletonClassic.class.getName()).log(Level.SEVERE, null, ex);
        }
        // init goes here.
        this.isReadyInstance = true;
    }
}
